package com.jeecg.p3.system.base;

import com.jeecg.p3.base.util.ComCode;
import com.jeecg.p3.base.util.UploadUtil;
import com.jeecg.p3.base.util.WxUserInfo;
import com.jeecg.p3.system.config.UploadConfig;
import com.jeecg.p3.system.entity.JwSystemUser;
import com.jeecg.p3.system.service.JwSystemUserService;
import com.jeecg.p3.system.util.Constants;
import com.jeecg.p3.system.util.HttpRequestDeviceUtils;
import com.jeecg.p3.system.util.QRCodeUtils;
import com.jeecg.p3.system.vo.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class BaseController {
    public final Logger log = LoggerFactory.getLogger(getClass());
    public static final String EXCEL_PATH_PREFIX = "WEB-INF/excel/";
    @Autowired
    protected UploadConfig uploadConfig;
    @Autowired
    protected JwSystemUserService userService;

    /**
     * 获取当前登录用户
     * @param request
     * @return
     */
    public LoginUser getCurrentUser(HttpServletRequest request) {
        LoginUser user = (LoginUser) request.getSession().getAttribute(Constants.OPERATE_WEB_LOGIN_USER);
        if(user == null) {
            WxUserInfo wxUserInfo = getWxUserInfo(request);
            if(wxUserInfo != null) {
                user = this.userService.queryUserByOpenid(wxUserInfo.getOpenid());
            }
        }
        return user;
    }

    /**
     * 判断是否为手机端
     * @param request
     * @return
     */
    public Boolean isMobile(HttpServletRequest request) {
        return HttpRequestDeviceUtils.isMobileDevice(request);
    }

    /**
     * 判断当前用户是否为管理员
     * @param request
     * @return
     */
    public Boolean isAdmin(HttpServletRequest request) {
        LoginUser user = getCurrentUser(request);
        JwSystemUser systemUser = this.userService.queryById(Long.valueOf(user.getId()));
        if(Constants.stateActvi.equals(systemUser.getAdmin())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取当前的微信用户信息
     * @param request
     * @return
     */
    public WxUserInfo getWxUserInfo(HttpServletRequest request) {
        Object wxInfo = request.getSession().getAttribute(Constants.wxInfo);
        if(wxInfo == null) {
            return null;
        } else {
            return (WxUserInfo) wxInfo;
        }
    }



    /**
     * 根据传参生成相应的二位码，返回map,包含可以访问到的网络路径，和文件实际保存地址
     * @param toUrl 跳转路径
     * @param fileUrl 文件访问路径
     * @param fileDir 文件保存路径
     * @param name 文件名称
     * @param needCompress 是否压缩
     * @return
     * @throws Exception
     */
     public UrlEntity createQrCode(String toUrl, String fileUrl, String fileDir, String name, Boolean needCompress) throws Exception {
         Map<String, String> map = new HashMap<>();
         UploadUtil.createDirectory(fileDir);//创建文件夹
         File newFile = UploadUtil.getAvailableFile(fileDir + name, 0);//判断文件名是否重复
         String realName = newFile.getName();//真正名称
         QRCodeUtils.encode(toUrl, null, fileDir + realName, needCompress);//生成二维码
        return new UrlEntity(fileDir + realName, fileUrl + realName);
     }

    /**
     * 单文件上传
     * @param fileUrl 网络地址
     * @param fileDir 实际路径地址
     * @param file 文件源
     * @return
     * @throws IOException
     */
     public String upload(String fileUrl, String fileDir, MultipartFile file) throws IOException {
         if(!file.isEmpty()) {
             String fileName = file.getOriginalFilename();
             UploadUtil.createDirectory (fileDir);
             String filePath = fileDir + fileName;
             File newFile = UploadUtil.getAvailableFile (filePath, 0);
             file.transferTo (newFile);
             return fileUrl + newFile.getName ();
         } else {
             return null;
         }
     }
}
